//
//  UIViewController+CreaeaNaviBar.m
//  ShunLianPower2
//
//  Created by 顺联网络 on 2017/11/29.
//  Copyright © 2017年 陈镇. All rights reserved.
//

#import "UIViewController+CreaeaNaviBar.h"
#import "SLBlockButton.h"
#import "UIButton+EnlargeEdge.h"
@implementation UIViewController (CreaeaNaviBar)

-(UIBarButtonItem *)createNaviBarItem:(NaviBatItemType)type image:(NSString *)imageName title:(NSString *)title action:(SEL)action color:(UIColor*)color{
    QMUIButton *btn = [[QMUIButton alloc] qmui_initWithImage:UIImageMake(imageName) title:title];
    
    btn.titleLabel.font = UIFontMake(15);
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *itm =  [[UIBarButtonItem alloc] initWithCustomView:btn];
    [btn sizeToFit];
//    float width = btn.frame.size.width;
    btn.frame = CGRectMake(0, 0, 44, 44);
    if (type == NaviBatItemTypeLeft) {
        [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        self.navigationItem.leftBarButtonItem =itm;
    }else if(type == NaviBatItemTypeRigth){
        [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        self.navigationItem.rightBarButtonItem =itm;
    }
    return itm;
}

-(UIBarButtonItem *)createNaviBarItem:(NaviBatItemType)type image:(NSString *)imageName title:(NSString *)title action:(SEL)action{
   return [self createNaviBarItem:type image:imageName title:title action:action color:UIColorHex(858585)];
}

-(void)createNaviBarItem:(NaviBatItemType)type items:(NSArray*)items block:(SLNaviItemBlick)block{
    
    NSMutableArray *buttonitems = [NSMutableArray array];
    for (int i = 0; i<items.count; i++) {
        NSDictionary *item = items[i];
        NSString *imageName = item[@"image"];
        NSString *title = item[@"title"];
        UIColor *color = item[@"color"];
        
        NSString *selectImageName = item[@"selectImage"];
        NSString *selectTitle = item[@"selectTitle"];
        UIColor *selectColor = item[@"selectColor"];
        
         QMUIButtonImagePosition imagePosition =[item[@"imagePosition"] integerValue];
        
        UIBarButtonItem *barItem =  [self createBarButtonitemImageName:imageName title:title color:color selectImageName:selectImageName selectTitle:selectTitle selectColor:selectColor tag:type*100+i imagePosition:imagePosition block:block];
      
        [buttonitems addObject:barItem];
    }
    
    for (UIBarButtonItem *item in buttonitems) {
        if ([item.customView isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton*)item.customView;
            if (type == NaviBatItemTypeLeft) {
                 btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            }else if(type == NaviBatItemTypeRigth){
                 btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            }
        }
    }
    if (type == NaviBatItemTypeLeft) {
        
        self.navigationItem.leftBarButtonItems = buttonitems;
    }else if(type == NaviBatItemTypeRigth){
        self.navigationItem.rightBarButtonItems =  [[buttonitems reverseObjectEnumerator] allObjects];
    }
    
}

-(UIBarButtonItem *)createBarButtonitemImageName:(NSString *)imageName title:(NSString *)title color:(UIColor *)color selectImageName:(NSString *)selectImageName selectTitle:(NSString *)selectTitle selectColor:(UIColor *)selectColor tag:(NSInteger)tag  imagePosition:(QMUIButtonImagePosition)imagePosition block:(SLNaviItemBlick)block{
    
    SLBlockButton *btn = [[SLBlockButton alloc] initWithBlock:^(SLBlockButton *button) {
        if (block) {
            NSInteger index = button.tag%100;
            block(button,index);
        }
    }];
    
    btn.titleLabel.font = UIFontMake(14);
    btn.tag = tag;
    btn.imagePosition = imagePosition;
    btn.spacingBetweenImageAndTitle = 4;
    
    if (color) {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    if (title) {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    if (imageName) {
        UIImage *image = [UIImage imageNamed:imageName];
        [image setAccessibilityIdentifier:imageName];
        [btn setImage:image forState:UIControlStateNormal];
    }
    
    if (selectColor) {
        [btn setTitleColor:selectColor forState:UIControlStateSelected];
    }
    if (selectTitle) {
        [btn setTitle:selectTitle forState:UIControlStateSelected];
    }
    if (selectImageName) {
        UIImage *image = [UIImage imageNamed:selectImageName];
        [image setAccessibilityIdentifier:selectImageName];
        [btn setImage:image forState:UIControlStateSelected];
    }
    if (imagePosition == QMUIButtonImagePositionTop && imageName && title) {
        
        btn.titleLabel.font = [UIFont systemFontOfSize:11];
    }
    UIBarButtonItem *itm =  [[UIBarButtonItem alloc] initWithCustomView:btn];
    [btn sizeToFit];
    float width = btn.frame.size.width;
    width = width<30?30:width;
    btn.frame = CGRectMake(0, 0, width+5, 30);
    [btn setEnlargeEdge:15];

    return itm;
}
-(UIButton *)getRigthMoreBotton{
    for (UIBarButtonItem *item in self.navigationItem.rightBarButtonItems) {
        if ([item.customView isKindOfClass:NSClassFromString(@"UIButton")]) {
            UIButton *btn = item.customView;
            if ([[btn.imageView.image accessibilityIdentifier] isEqualToString:@"icon_common_more_black"] || [[btn.imageView.image accessibilityIdentifier] isEqualToString:@"icon_common_more_white"]) {
                return btn;
            }
            
        }
    }
    return nil;
}
-(UIButton *)getRigthMoreBottonTag:(NSInteger)tag{
    for (UIBarButtonItem *item in self.navigationItem.rightBarButtonItems) {
        if ([item.customView isKindOfClass:NSClassFromString(@"UIButton")]) {
            UIButton *btn = item.customView;
            if ( btn.tag%100 == tag) {
                return btn;
            }
            
        }
    }
    return nil;
}




@end
